package com.example.alipay.common;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 返回结果集
 * @param <T>
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
public class ResultResponse<T> implements Serializable {

    private static final long serialVersionUID = 101539585470879542L;

    public static final int CODE_SUCCESS = 0;
    public static final String MSG_SUCCESS = "操作成功";


    private T data;

    private int code;

    private String msg;

    public ResultResponse(T data) {
        super();
        this.code = CODE_SUCCESS;
        this.msg = MSG_SUCCESS;
        this.data = data;
    }


    public ResultResponse(int code, String msg) {
        super();
        this.code = code;
        this.msg = msg;
    }

    public ResultResponse(int code, String msg, String errorMsg) {
        super();
        this.code = code;
        this.msg = msg;
    }

    public ResultResponse(int code, String msg, T data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultResponse(int code, String msg, String errorMsg, T data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    //新增强化参数
    /**
     * 成功
     *
     * @param msg  成功消息
     * @param data 返回的数据
     * @author:
     */
    public static <T> ResultResponse succeed(String msg, T data) {
        return new ResultResponse(ResponseStatus.SUCCESS.value(), msg, "", data);
    }

    /**
     * 成功
     *
     * @param data 返回的数据
     * @author:
     */
    public static <T> ResultResponse succeed(T data) {
        return new ResultResponse(ResponseStatus.SUCCESS.value(), ResponseStatus.SUCCESS.getReasonPhrase(), "", data);
    }

    /**
     * 成功
     *
     * @author:
     */
    public static ResultResponse succeed() {
        return succeed(null);
    }


    /**
     * 失败
     *
     * @param msg  失败消息
     * @param data 失败数据
     * @author:
     */
    public static <T> ResultResponse failed(int code, String msg, T data) {
        code = code == ResponseStatus.SUCCESS.value() ? ResponseStatus.OTHER_ERROR.value() : code;
        return new ResultResponse(code, msg, "", data);
    }

    /**
     * 失败
     *
     * @param msg  失败消息
     * @param data 失败数据
     * @author:
     */
    public static <T> ResultResponse failed(int code, String msg, String stackTrace, T data) {
        code = code == ResponseStatus.SUCCESS.value() ? ResponseStatus.OTHER_ERROR.value() : code;
        return new ResultResponse(code, msg, stackTrace, data);
    }

    /**
     * 失败
     *
     * @param msg  失败消息
     * @param data 失败数据
     * @author:
     */
    public static <T> ResultResponse failed(String msg, T data) {
        return failed(ResponseStatus.OTHER_ERROR.value(), msg, data);
    }

    /**
     * 失败
     *
     * @param msg 失败消息
     * @author:
     */
    public static <T> ResultResponse failed(String msg) {
        return failed(msg, null);
    }

}